package com.yjhb.android.sbz.tool;

import com.yjhb.android.sbz.entity.SystemNotice;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack;
import com.tencent.qcloud.tim.uikit.modules.chat.C2CChatManagerKit;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatProvider;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2021/1/19 10:30
 *
 * 设置消息已读
 */
public class MsgReadTool {
    private final String TAG = "MsgReadTool";

    public void loadSystemNotice(String sysId) {
        ChatInfo chatInfo = new ChatInfo();
        chatInfo.setType(V2TIMConversation.V2TIM_C2C);
        chatInfo.setId(sysId);
        chatInfo.setChatName("");
        C2CChatManagerKit.getInstance().setCurrentChatInfo(chatInfo);
        C2CChatManagerKit.getInstance().loadChatMessages(null, new IUIKitCallBack() {
            @Override
            public void onSuccess(Object data) {
                ChatProvider chatProvider = (ChatProvider) data;
                List<SystemNotice> systemNotices = new ArrayList<>();
                if (chatProvider != null) {
                    for (MessageInfo messageInfo : chatProvider.getDataSource()
                    ) {
                        SystemNotice systemNotice = new SystemNotice();
                        systemNotice.setContent("" + messageInfo.getExtra());
                        systemNotice.setTime(new Date(messageInfo.getMsgTime() * 1000));
                        systemNotice.setMessageInfo(messageInfo);
                        systemNotices.add(systemNotice);
                    }
                }
                //按时间排序
                Collections.sort(systemNotices, new Comparator<SystemNotice>() {
                    @Override
                    public int compare(SystemNotice o1, SystemNotice o2) {
                        long time1 = o1.getTime().getTime();
                        long time2 = o2.getTime().getTime();
                        if (time1 < time2) {
                            return 1;
                        }
                        if (time1 > time2) {
                            return -1;
                        }
                        return 0;
                    }
                });
            }

            @Override
            public void onError(String module, int errCode, String errMsg) {

            }
        });
    }

    public void destroyChat(){
        C2CChatManagerKit.getInstance().destroyChat();
    }
}
